﻿<UserControl x:Class="CodeIDX.Views.Resources.Controls.EditableAvalonEditor"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:Controls="clr-namespace:CodeIDX.Views.Resources.Controls"
             xmlns:Converters="clr-namespace:CodeIDX.Views.Resources.Converters"
             xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             d:DesignHeight="300"
             d:DesignWidth="300"
             mc:Ignorable="d">
    <UserControl.CommandBindings>
        <CommandBinding CanExecute="Save_CanExecute"
                        Command="ApplicationCommands.Save"
                        Executed="Save_Executed" />
    </UserControl.CommandBindings>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <avalonEdit:TextEditor x:Name="PreviewEditor"
                               Grid.RowSpan="2"
                               Grid.ColumnSpan="2"
                               DataContext="{Binding}"
                               DataContextChanged="AvalonEditor_DataContextChanged"
                               HorizontalScrollBarVisibility="Auto"
                               IsReadOnly="{Binding IsEditEnabled,
                                                    RelativeSource={RelativeSource Mode=FindAncestor,
                                                                                   AncestorType={x:Type Controls:EditableAvalonEditor}},
                                                    Converter={x:Static Converters:BoolToReversedBoolConverter.Instance}}"
                               ShowLineNumbers="True" />

        <StackPanel Grid.Row="1"
                    Grid.Column="1"
                    Margin="0,0,20,0"
                    Orientation="Horizontal">
            <!--
                <Button>
                <Button.Content>
                <Image Width="16"
                Height="16"
                Source="pack://application:,,,/CodeIDX;component/Views/Resources/Images/edit.png" />
                </Button.Content>
                </Button>
            -->

            <Button x:Name="btnSave"
                    Command="ApplicationCommands.Save"
                    DataContext="{Binding}">
                <Button.Style>
                    <Style BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
                           TargetType="{x:Type Button}">
                        <Setter Property="Opacity" Value="0.5" />
                        <Setter Property="Margin" Value="3,2" />
                        <Setter Property="Focusable" Value="False" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding}"
                                         Value="{x:Null}">
                                <Setter Property="Visibility" Value="Collapsed" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding IsEditEnabled,
                                                           RelativeSource={RelativeSource Mode=FindAncestor,
                                                                                          AncestorType={x:Type Controls:EditableAvalonEditor}}}"
                                         Value="False">
                                <Setter Property="Visibility" Value="Collapsed" />
                            </DataTrigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Trigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation Duration="0:0:0.2"
                                                             Storyboard.TargetProperty="Opacity"
                                                             To="1" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </Trigger.EnterActions>
                                <Trigger.ExitActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation Duration="0:0:0.2"
                                                             Storyboard.TargetProperty="Opacity"
                                                             To="0.5" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </Trigger.ExitActions>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
                <Button.Content>
                    <Image Width="16"
                           Height="16"
                           Source="pack://application:,,,/CodeIDX;component/Views/Resources/Images/save.png" />
                </Button.Content>
            </Button>
        </StackPanel>

    </Grid>
</UserControl>
